package voip.capture.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import cs252.voip.R;

import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder.AudioSource;

public class VoiceCaptureClient extends Activity
{
	Worker w = new Worker();

	  @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        this.setContentView(R.layout.in_call);

		
		Thread t = new Thread(w);
		t.start();
		
		Button closeButton = (Button)this.findViewById(R.id.button1);
		closeButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();	
			}
		});
	}
	  
	  
	@Override
	public void onDestroy() {
		w.stop();

		super.onDestroy();
	}

	public class Worker implements Runnable {
		private boolean stopped = false;

		static final int AUDIO_PORT_LOCAL = 4446; //Incoming
		static final int AUDIO_PORT_REMOTE = 4449; //Incoming

	 //   static final int SAMPLE_RATE = 8000;
	 //   static final int SAMPLE_INTERVAL = 20; // milliseconds
	//    static final int SAMPLE_SIZE = 2; // bytes per sample
	    
	    
		private int samplingRate = 44100; /* in Hz*/
		private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
		private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
	    final int BUF_SIZE =  samplingRate;



		@Override
		public void run() {

            Log.e("CLIENT", "CLIENT STARTED"
                + Thread.currentThread().getId());
            AudioTrack track = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 
                   samplingRate,channelConfig, 
                    audioFormat, BUF_SIZE, 
                    AudioTrack.MODE_STREAM);
            try
            {
        		Bundle extras = getIntent().getExtras();
    			InetAddress inet = InetAddress.getByName(extras.getString("User_IP"));
    			
                DatagramSocket sock = new DatagramSocket(AUDIO_PORT_LOCAL);	//Socket created
                sock.connect(inet, AUDIO_PORT_REMOTE);	//Connect to the server on the other persons phone
                
                byte[] buf = new byte[BUF_SIZE];

				Log.d("Server", "We are Receiving  from " + extras.getString("User_IP") );

                while(stopped == false)
                {
                    DatagramPacket pack = new DatagramPacket(buf, BUF_SIZE);
                 //   Log.d("VOICE CAPTURE SERVER","Trying to receive packet");
                    sock.receive(pack);
                 //   Log.d("VOICE CAPTURE SERVER", "Received Packet" + pack.getLength());
                    track.write(pack.getData(), 0, pack.getLength());
                    track.play();

                }
                /*Stop recording, close socket*/
             sock.close();
             track.stop();
            }
            catch (SocketException se)
            {
                Log.e("VOICE CAPTURE SERVER", "SocketException: " + se.toString());
            }
            catch (IOException ie)
            {
                Log.e("VOICE CAPTURE SERVER", "IOException" + ie.toString());
            }

		}

		protected void stop()
		{ 
			stopped = true;
		}

	}
}



















